package com.controller;

import com.github.pagehelper.PageInfo;
import com.mapper.ReplyDetailMapper;
import com.pojo.ReplyDetail;
import com.service.ServiceInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

/**
 * @author Aiden
 * @version 1.0
 * @description
 * @date 2019-12-11 12:59:27
 */
@Controller
@RequestMapping("/main")
public class MainController {
    @Autowired
    ServiceInterface serviceInterface;

    @RequestMapping("/list")
    public ModelAndView list(@RequestParam(value = "ctitle", required = false) String title,
                                                    @RequestParam(defaultValue = "1") Integer pageNum,
                                                     ModelAndView mv){
        PageInfo pageInfo = serviceInterface.findAllInvitation(title, pageNum, 3);
        mv.addObject("pageInfo", pageInfo);
        mv.addObject("title", title);
        mv.setViewName("/jsp/main.jsp");
        return mv;
    }

    @RequestMapping("/reply/{id}/{pageNum}")
    public ModelAndView reply(@PathVariable("id") Integer invid,
                                                         @PathVariable("pageNum") Integer pageNum,
                                                          ModelAndView mv){
        PageInfo pageInfo = serviceInterface.findAllReplyDetail(invid, pageNum, 3);
         mv.addObject("invid", invid);
         mv.addObject("pageInfo", pageInfo);
         mv.setViewName("/jsp/com.jsp");
         return mv;
     }

     @Autowired
     ReplyDetail replyDetail;
     //添加
    @RequestMapping("/add")
    public ModelAndView addreply(ReplyDetail replyDetail, ModelAndView mv){
        replyDetail.setCreatedate(new Date());
        if(replyDetail.getAuthor().equals("") || replyDetail.getAuthor()==null){
            replyDetail.setAuthor("匿名用户");
        }
        boolean b = serviceInterface.addReply(replyDetail);
        if(b){
            mv.addObject("msg01", "success");
        }else{
            mv.addObject("msg01", "fail");
        }
        mv.setViewName("/main/reply/"+replyDetail.getInvid());
        return mv;
    }

    //删除
    @RequestMapping("/remove/{id}")
    public ModelAndView deleteInvitation(@PathVariable("id") Integer id, ModelAndView mv){
        boolean b = serviceInterface.removeInvitationAndReply(id);
        if(b){
            mv.addObject("msg02", "success");
        }else{
            mv.addObject("msg02", "fail");
        }
        mv.setViewName("/main/list");
        return mv;
    }
}





















